package talvan;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import java.sql.*;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

public class GUILogin extends javax.swing.JFrame {

    private DbHandler dbh = null;

    public GUILogin(DbHandler _dbh) {
        this.dbh = _dbh;
        initComponents();
    }
    
    /**
     * Metoda til tá man trýstir á login knøtin
     */
    public void login() {
        java.sql.DatabaseMetaData dm = null;
        java.sql.ResultSet rs = null;
        try {
            if (dbh.getConnection() != null) {
                
                String username = brukariField.getText();
                String password = loyniordField.getText();
                String sql = "USE TALVAN;";
                sql += "SELECT employe_id FROM LoginUser WHERE employeUserName = '" + username + "' AND employeLoginPassword = '" + password + "'";
                java.sql.Statement stmt = dbh.getConnection().createStatement();
                rs = stmt.executeQuery(sql);
                
                int brúkaraID = -1;

                while (rs.next()) {
                    brúkaraID = Integer.parseInt(rs.getString("employe_id"));
                }
                
                if (brúkaraID > 0) {
                    startDataHandler(brúkaraID);
                    close();
                } else {
                    JOptionPane.showMessageDialog(this, "Brúkari ella Loyniord er ikki rætt");
                }

                
                rs.close();
                rs = null;
                dbh.closeConnection();
            } else {
                System.out.println("Error: No active Connection");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e);
            e.printStackTrace();
        }
        dm = null;
    }

    /**
     * Metoda sum opnar eitt nýtt panel til
     */
    private void setSettings()
    {
        JDialog jd = new JDialog(this, "Connections Settings", true);
        jd.setContentPane(new GUIDBHsettings(dbh,jd));
        jd.pack();
        jd.setVisible(true);
    }
    
    /**
     * Metoda sum startar forriti
     * @param int ID
     */
    private void startDataHandler(int userID) {
        DataHandler data = new DataHandler(userID, dbh);
        GUIMain gui = new GUIMain(data);
        gui.setTitle("Brúkara ID :  " + userID);
        gui.setVisible(true);
        //this.dispose();
    }
    
    /**
     * Metoda sum sløkkir hesa markaótina
     */
    private void close()
    {
        this.dispose();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        brukariField = new javax.swing.JTextField();
        loyniordField = new javax.swing.JTextField();
        llbl = new javax.swing.JLabel();
        blbl = new javax.swing.JLabel();
        loginLbl = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        setingsButton = new javax.swing.JButton();
        loginButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        brukariField.setText("brukari");

        loyniordField.setText("1234");

        llbl.setText("Loyni orð");

        blbl.setText("Brúkari");

        loginLbl.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        loginLbl.setText("Login");

        jLabel1.setText("Status:");

        setingsButton.setText("Settings");

        loginButton.setText("Log inn");
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(blbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(llbl, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(brukariField)
                            .addComponent(loyniordField)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(loginButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                        .addComponent(setingsButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(loginLbl)
                        .addGap(28, 28, 28)
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginLbl)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(blbl)
                    .addComponent(brukariField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(llbl)
                    .addComponent(loyniordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(setingsButton)
                    .addComponent(loginButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginButtonActionPerformed
        login();
    }//GEN-LAST:event_loginButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        close();
    }//GEN-LAST:event_cancelButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel blbl;
    private javax.swing.JTextField brukariField;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel llbl;
    private javax.swing.JButton loginButton;
    private javax.swing.JLabel loginLbl;
    private javax.swing.JTextField loyniordField;
    private javax.swing.JButton setingsButton;
    // End of variables declaration//GEN-END:variables
}
